package w4156.idog.client;


import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;

import w4156.idog.exception.DBexception;
import w4156.idog.model.User;
import w4156.idog.server.PlayerManagerBeanRemote;
import w4156.idog.server.TransactionManagerBeanRemote;
import w4156.idog.server.UserSessionRemote;


public class TestClient {
		@EJB
		private static UserSessionRemote test;
		private static TransactionManagerBeanRemote transactionManager;
		private static PlayerManagerBeanRemote playerManager;
		public TestClient(){
		}

		private void runTest() throws Exception {
			Context playerCtx;

			playerCtx = new InitialContext();
		    playerManager = (PlayerManagerBeanRemote) playerCtx.lookup("ejb/eBlackjack/PlayerManagerSLSB");

			System.out.println("Saving user ..... " );

			try {
				User user = new User();
				user.setName("ZH");
				user.setPassword("zh");
				user.setObjid(99999);
				int id = playerManager.createUser(user);
				System.out.println("Saving user successfully: " + id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		public static void main(String[] args) throws Exception{
			if (true) {
				TestClient cli = new TestClient();
				cli.runTest();
				return;
			}
			
			User one = null;
			
			try{
			InitialContext transactionCtx = new InitialContext();
			transactionManager = (TransactionManagerBeanRemote) transactionCtx.lookup("ejb/eBlackjack/TransMgrSLSB");
//			playerManager = (PlayerManagerBeanRemote) transactionCtx.lookup("ejb/eBlackjack/PlayerManagerSLSB");
			System.out.println("EJB connected successfully!");
			User u = transactionManager.authenticate("admin", "password");
//			User u = new User();
/*			u.setName("testUser1");
			u.setPassword("password");
			u.setObjid(100000000);*/
//			int user = transactionManager.createUser(u);
//			User user = playerManager.findByPK(1);

			if (u == null)
				throw new RuntimeException("Authentication failed");
			else 
				System.out.println("OK");
			
//			List<User> users = test.findAllContacts();
//			one = users.get(0);
/*			List<User> a = test.findAllContacts();
			for (Integer i=0;i<a.size();i++)
			{
				User u = a.get(i);
				System.out.println("Name: "+u.getName()+"    Password: "+u.getPassword());
			}*/
//			one = test.findByName("admin").get(0);
//			one = test.findByPK(1);

			
			} catch (Exception ex){
				ex.printStackTrace();
				System.out.println("MyERROR: "+ex.getMessage());
			}
			
/*			if (one != null)
				System.out.println("Name: "+one.getName()+"    Password: "+one.getPassword());
			else {
				User u = new User();
				u.setName("admin");
				u.setPassword("password");
				u.setCity("");
				u.setAddress("");
				u.setCountry("");
				u.setZipcode("");
				try {
					test.save(u);
				} catch (Exception e) {
					System.out.println("saving exception:");
					e.printStackTrace();
				}
			}*/

		}
}
